<?php
namespace app\admin\controller;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Cate
{
    public function cate()
    {
        $parentid=Request::get('id',0);
        $cate=Db::name('category')->order('sort')->Where('parentid','=',$parentid)->select()->toArray();


        //方案一:按照父ID进行判断
//        $er=Db::name('category')->distinct(true)->column('parentid');
//        View::assign('er',$er);
//        foreach ($cate as $k=>$v){
//            $cate[$k]['son'] = Db::name('category')->where('parentid','=',$v['id'])->count();
//        }

        if (Cache::has('all')){
           $all=Cache::get('all');
        }else{
           $all=Db::name('category')->select()->toArray();
            Cache::set('all',$all,10);
        }
        foreach ($cate as &$v){
            foreach ($all as $vv){
                if($v['id']==$vv['parentid']){
                    $v['son']=1;
                }
            }
        }
        View::assign('cate',$cate);
        return View();
    }
}